﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Phone.Controls;
using MvvmLight.Messaging;
using System.Windows;
using Coding4Fun.Phone.Controls;

namespace AccountHelper.View
{
    public partial class NewChargeView : PhoneApplicationPage
    {
        public NewChargeView()
        {
            InitializeComponent();
            Messenger.Default.Register<bool>(this,"GoBack", (p) =>
            {
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
            });
            Loaded += new RoutedEventHandler(NewChargeView_Loaded);
            this.ptbChargeTitle.GotFocus += new RoutedEventHandler(ptbChargeTitle_GotFocus);
            this.ptbAmount.GotFocus += new RoutedEventHandler(ptbAmount_GotFocus);
        }

        void ptbChargeTitle_GotFocus(object sender, RoutedEventArgs e)
        {
            //this.ptbChargeTitle.
        }

        void ptbAmount_GotFocus(object sender, RoutedEventArgs e)
        {
            this.ptbAmount.SelectAll();
        }

        void NewChargeView_Loaded(object sender, RoutedEventArgs e)
        {
            this.ptbChargeTitle.Focus();
        }

        private void AppBar_Save(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.ptbChargeTitle.Text))
            {
                ToastPrompt toast = new ToastPrompt
                {
                    Message="费用名称不能为空",
                };
                toast.Show();
                return;
            }

            if (string.IsNullOrEmpty(this.ptbAmount.Text))
            {
                ToastPrompt toast = new ToastPrompt
                {
                    Message = "费用金额不能为空",
                };
                toast.Show();
                return;
            }

            ViewModelLocator.Instance.NewChargeVM.SaveCommand.Execute(null);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            Messenger.Default.Unregister<bool>(this, "GoBack");
            //reset deafult value
            ViewModelLocator.Instance.NewChargeVM.ResetCommand.Execute(null);
            base.OnNavigatedFrom(e);
        }

        
    }
}